<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>成绩查询</title>
    <script type="text/javascript" th:src="@{/static/layui/layui.js}"></script>
    <link rel="stylesheet" type="text/css" th:href="@{/static/layui/css/layui.css}"/>
</head>
<body>
<div class="layui-form">
    <div class="layui-inline" style="margin-top: 10px;margin-left: 30px">
        班级：
        <div class="layui-input-inline">
            <select  name="classid" id="classid" lay-search="" lay-verify="stime"  lay-filter="stime">
                <option value="" disabled selected hidden>请选择</option>
                <option th:each="c:${tchClass}" th:value="${c.classid}" th:text="${c.classname}"></option>
            </select>
        </div>
        年级：
        <div class="layui-input-inline">
            <select  name="gname" id="gname" lay-search="" lay-verify="stime"  lay-filter="stime">
                <option value="" disabled selected hidden>请选择</option>
                <option th:each="g:${tchGrade}" th:value="${g.gname}" th:text="${g.gname}"></option>
            </select>
        </div>
        学生姓名：
        <div class="layui-inline">
            <input class="layui-input" name="sname" id="sname" autocomplete="off">
        </div>
    </div>

<button class="layui-btn" type="button" id="selBtn" style="margin-top: 10px">开始搜索</button>
</div>
<table class="layui-hide" id="userTable" lay-filter="userTable"></table>
<!--修改成绩的弹出层-->
<div style="display: none;padding: 20px" id="saveOrUpdateDiv" >
    <form class="layui-form"  lay-filter="dataFrms" id="dataFrms">
        <div class="layui-form-item">
            <label class="layui-form-label">姓名</label>
            <div class="layui-input-inline">
                <input type="text" name="sname" id="snames"  lay-verify="required" readonly="true" placeholder="请输入" autocomplete="off" class="layui-input">
            </div>


            <label class="layui-form-label">科目</label>
            <div class="layui-input-inline">
                <input type="text" name="cname"  lay-verify="required" readonly="true" placeholder="请输入" autocomplete="off" class="layui-input">

            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">平时成绩</label>
            <div class="layui-input-inline">
                <input type="text" name="normalexam" id="normalexams"  lay-verify="required" placeholder="请输入" autocomplete="off" class="layui-input">
            </div>
            <label class="layui-form-label">考试成绩</label>
            <div class="layui-input-inline">
                <input type="text" name="testexam" id="testexams"  lay-verify="required" placeholder="请输入" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">综合成绩</label>
            <div class="layui-input-inline">
                <input type="text" name="sumexam" id="sumexams"  lay-verify="required" readonly="true" placeholder="请输入" autocomplete="off" class="layui-input">
            </div>
            <div class="layui-input-inline" style="display: none">
                <select name="stime" id="stimes" onfocus="blur()" lay-verify="required">
                    <option value="1">大一上学期</option>
                    <option value="2">大一下学期</option>
                    <option value="3">大二上学期</option>
                    <option value="4">大二下学期</option>
                    <option value="5">大三上学期</option>
                    <option value="6">大三下学期</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-inline" style="margin-left: 200px;margin-top: 50px">
                <button type="button" class="layui-btn layui-btn-primary" lay-filter="formDemos" lay-submit>提交</button>
            </div>
        </div>
    </form>
</div>
<script>

    layui.use(['laydate', 'laypage', 'layer', 'table', 'carousel', 'upload', 'element', 'slider', 'jquery', 'form'], function() {
        var laydate = layui.laydate ,//日期
            laypage = layui.laypage, //分页
            layer = layui.layer, //弹层
            table = layui.table, //表格
            carousel = layui.carousel, //轮播
            upload = layui.upload, //上传
            element = layui.element, //元素操作
            slider = layui.slider,//滑块
            form = layui.form;
        var mainIndex;
        //获取分页数据列表
        table.render({
            elem: '#userTable'
            , url: '/tchSelStuExam'
            , cols: [
                [
                    {field: 'sid', title: '学号'}
                    ,{field: 'sname', title: '学生'}
                    ,{field: 'cname', title: '课程'}
                    ,{field: 'stime', title: '学期',templet: function (data) {
                        if (data.stime =='1'){
                            return '大一上学期'
                        }else if (data.stime =='2'){
                            return '大一下学期'
                        }else if (data.stime =='3'){
                            return '大二上学期'
                        }else if (data.stime =='4'){
                            return '大二下学期'
                        }else if (data.stime =='5'){
                            return '大三上学期'
                        }else if (data.stime =='6'){
                            return '大三下学期'
                        }
                    }}
                    ,{field: 'ccredit', title: '学分'}
                    ,{field: 'ctname', title: '类别'}
                    ,{field: 'studytype', title: '修读性质'}
                    ,{field: 'normalexam', title: '平时成绩'}
                    ,{field: 'testexam', title: '考试成绩'}
                    ,{field: 'sumexam', title: '综合成绩'}
                    ,{field: '', title: '操作',templet:function(data){
                        return '<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="update">修改成绩</a>'
                    }}
                ]
            ]
            , page: true
            , even: true
            , id: 'tchSelStuExam'
        });
        //监听行工具事件
        table.on('tool(userTable)', function(obj){
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            if(layEvent == 'update'){
                openUpdate(data);
            }
        });
        //修改
        //打开修改页面
        function  openUpdate(data){
            mainIndex=layer.open({
                type:1,
                title:'修改',
                content:$("#saveOrUpdateDiv"),
                area:['800px','350px'],
                success:function(index){
                    form.val("dataFrms",data);
                    form.on('submit(formDemos)', function(){
                        var normalexam=$("#normalexams").val();
                        var testexam=$("#testexams").val();
                        var sumexam=$("#sumexams").val();
                        var stime=$("#stimes").val();
                        $.post("/saveupdate",{scid:data.scid,normalexam:normalexam, testexam:testexam, sumexam:sumexam, stime:stime},function(res){
                            layer.msg(res)
                            window.setTimeout('window.location.href="/tchSelExam"',2000);
                        })
                    })
                }

            });
            $("#normalexams").on('blur',function () {
                var  nexamregs= /^(([^0][0-9]+|0)\.([0-9]{1,2})$)|^(([^0][0-9]+|0)$)|^(([1-9]+)\.([0-9]{1,2})$)|^(([1-9]+)$)/
                var normalexams=$("#normalexams").val();
                var testexams=$("#testexams").val();
                if(normalexams>100||normalexams<0){
                    layer.msg("最低分为0，最高分为100")
                    $("#normalexams").val("");
                }
                else if(!nexamregs.test(normalexams)&&normalexams!=""){
                    layer.msg("只能为数字")
                    $("#normalexams").val("");
                }
            })
            $("#testexams").on('blur',function () {
                var  texamregs= /^(([^0][0-9]+|0)\.([0-9]{1,2})$)|^(([^0][0-9]+|0)$)|^(([1-9]+)\.([0-9]{1,2})$)|^(([1-9]+)$)/
                var normalexams=$("#normalexams").val();
                var testexams=$("#testexams").val();
                if(testexams>100||testexams<0){
                    layer.msg("最低分为0，最高分为100")
                    $("#testexams").val("");
                }
                else if(!texamregs.test(testexams)&&testexams!=""){
                    layer.msg("只能为数字")
                    $("#testexams").val("");
                }
                else  if (testexams==""){
                    $("#sumexams").val("")
                }
                else if(testexams!="")
                {$("#sumexams").val((parseFloat(normalexams)+parseFloat(testexams))/2);
                }
            })
        }

        //  按条件查询成绩
        var $ = layui.$;
        $('#selBtn').on('click', function(){
            //获取输入框
            var gname = $('#gname');
            var classid = $('#classid');
            var sname = $('#sname');
            //执行重载
            table.reload('tchSelStuExam', {
                page: {
                    curr: 1 //重新从第 1 页开始selTeacher
                }
                ,where: {
                    gname: gname.val(),
                    sname: sname.val(),
                    classid: classid.val()
                }
            });
        });

    });
</script>
</body>
</html>